package org.example.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class HandleRegisterUser implements ActionListener {
    public RegisterUserView registerUserView;

    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;

    public void setRegisterUserView(RegisterUserView registerUserView) {
        this.registerUserView = registerUserView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerUserView.submit){
            InetAddress address= null;
            socket=new Socket();
            thread=new Thread(registerUserView);
            try {
                address = InetAddress.getByName("127.0.0.1");
                InetSocketAddress socketAddress=new InetSocketAddress
                        (address,4331);
                socket.connect(socketAddress);
                in =new DataInputStream(socket.getInputStream());
                out = new DataOutputStream(socket.getOutputStream());
                thread.start();
            } catch (IOException ex) {
                socket=new Socket();
            }
            String s ="查询ID:" + registerUserView.inputID.getText();        //处理登陆数据
            s +="姓名：类型：密码：" + registerUserView.inputPassword.getText();
            if (s!=null){
                try{
                    out.writeUTF(s);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        } else if (e.getSource() == registerUserView.login) {
            registerUserView.setVisible(false);
            new AddUserView();
        }

    }
}
